﻿using AutoMapper;
using Infra_Common.redis;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using xiaodingmarket_application.Interfaces;
using xiaodingmarket_application.ViewModel;
using xiaodingmarket_Domain.Commands;
using xiaodingmarket_Domain.Commands.User;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain.Models;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Infra_Data.Repository.EventStore;
using static xiaodingmarket_Domain.Models.EunmType;

namespace xiaodingmarket_application.Service
{
    public class ManageService : IManageService
    {
        private readonly IMapper _mapper;
        private readonly ICommodityRepository _commodityRepository;
        private readonly IUserRepository _userRepository;
        private readonly IEventStoreRepository _eventStoreRepository;
        private readonly IMediatorHandler Bus;
        private readonly RedisClient redis;

        public ManageService(IMapper mapper,
                                  ICommodityRepository commodityRepository,
                                  IMediatorHandler bus,
                                  IEventStoreRepository eventStoreRepository,
                                  IUserRepository userRepository,
                                  RedisClient reviewRedis)
        {
            _userRepository = userRepository;
            _mapper = mapper;
            _commodityRepository = commodityRepository;
            Bus = bus;
            _eventStoreRepository = eventStoreRepository;
            redis = reviewRedis;
        }
        /// <summary>
        /// 获取未审核的商品返回给管理员进行审核,Cqrs读写分离，不用命令事件
        /// </summary>
        /// <param name="num">管理员想审核的数量</param>
        /// <param name="Commoditystate">枚举-reviewing = 1,</param>
        /// <returns></returns>
        public IEnumerable<CommodityViewModel> GetAny(int num)
        {
            //从redis中获取num条key值,"ReviewingCommodity_"  参数暂时没用了
            List<string> keys = redis.GetAnyKeySyn(num, "ReviewingCommodity_");
            //存储商品id的集合
            List<Guid> newkeys = new List<Guid>();
            foreach (string key in keys)
            {
                var id = new Guid(key.Replace("ReviewingCommodity_", ""));
                var id1 = Guid.Parse(key.Replace("ReviewingCommodity_", ""));
                newkeys.Add(id);
            }
            //批量更改键值
            if (!redis.UpdatekeySyn(keys))
                return null;
            //返回商品信息
            var commdity = _commodityRepository.GetAny(newkeys);
            return _mapper.Map<IEnumerable<CommodityViewModel>>(commdity);


        }
        /// <summary>
        /// 管理员将审核结果一个一个发回，对数据库以及redis进行更新
        /// </summary>
        public void Update(CommodityViewModel commdityViewModel)
        {
            //审核通过，删除redis对应的值，以及发送命令修改数据库状态
            redis.Remove("onlock_" + "ReviewingCommodity_" + commdityViewModel.CommodityId.ToString());
            var ReviewCommand = _mapper.Map<ReviewCommodityCommand>(commdityViewModel);
            Bus.SendCommand(ReviewCommand);
        }
        


        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }
        //超级管理员任命普通管理员
        public void TobeAdmin(string studentid)
        {
            Bus.SendCommand(new BeAdminCommand(studentid));
        }
    }
}
